<?php
/**
 * User: itimphp
 * Date: 6/4/12
 * Time: 10:24 PM
 */

require_once 'bootstrap.php';

if (!empty($_GET['msg'])) {
	$messages = array(
		1 => 'Captcha need to be filled. Please type the words extract the image!',
		2 => 'The reCAPTCHA wasn\'t entered correctly. Try it again.',
	);

	if (array_key_exists(intval($_GET['msg']), $messages)) {
		echo '<div class="alert alert-error">' . $messages[intval($_GET['msg'])] . '</div>';
	}
}

if (!empty($_POST)) {
	if (empty($_POST["recaptcha_challenge_field"]) || empty($_POST["recaptcha_response_field"])) {
		// Redirect to form1_view.php with error message: Recaptcha need to be filled!
		header('Location: form1.php?msg=1', true, 302);
	} else {
		$privatekey = '6LdkS9ISAAAAAJyUfyz-tRpCsTIU84A5GmNkox-F';
		$resp = recaptcha_check_answer($privatekey,
			$_SERVER["REMOTE_ADDR"],
			$_POST["recaptcha_challenge_field"],
			$_POST["recaptcha_response_field"]);

		if (!$resp->is_valid) {
			// What happens when the CAPTCHA was entered incorrectly
			header('Location: form1.php?msg=2', true, 302);
		} else {
			// Validation array
			$validations = array(
				'first_name' => array('type' => 'anything', 'message' => 'First Name must..'),
				'last_name' => array('type' => 'anything', 'message' => 'Last Name must..'),
				'email' => array('type' => 'email', 'message' => 'Email must be in format name@company.com'),
				'phone' => array('type' => 'phone', 'message' => 'Phone must have 9-11 digital numbers'),
				'postcode' => array('type' => 'anything'),
				'requested_amount' => array('type' => 'number'),
				'title' => array('type' => 'anything'),
				'streetnumber' => array('type' => 'anything'),
				'town' => array('type' => 'anything'),
				'country' => array('type' => 'anything'),
				'own_home' => array('type' => 'anything'),
				'home_phone' => array('type' => 'phone'),
				'work_phone' => array('type' => 'phone'),
				'mobile_phone' => array('type' => 'phone'),
				'address_length' => array('type' => 'anything'),
				'birth_date' => array('type' => 'date'),
				'income_type' => array('type' => 'anything'),
				'employer' => array('type' => 'anything'),
				'job_title' => array('type' => 'anything'),
				'pay_frequency' => array('type' => 'anything'),
				'next_pay_date' => array('type' => 'date'),
				'pay_date_after' => array('type' => 'date'),
				'employed_time' => array('type' => 'date'),
				'monthly_income' => array('type' => 'anything'),
				'bank_name' => array('type' => 'anything'),
				'bank_account_number' => array('type' => 'anything'),
				'bank_sort_code' => array('type' => 'anything'),
				'debit_card' => array('type' => 'number'),
				'direct_deposit' => array('type' => 'number'),
				'bank_account_length_months' => array('type' => 'number'),
			);
			// Required array
			$required = array('first_name', 'last_name', 'email', 'phone', 'postcode', 'requested_amount',
				'title', 'streetnumber', 'town', 'country', 'own_home', 'work_phone',
				'mobile_phone', 'address_length', 'birth_date', 'income_type', 'employer', 'job_title',
				'pay_frequency', 'last_name', 'email', 'phone', 'postcode', 'requested_amount',
				'first_name', 'next_pay_date', 'pay_date_after', 'employed_time', 'monthly_income', 'bank_name',
				'bank_sort_code', 'debit_card', 'direct_deposit', 'bank_account_length_months'
			);
			// Loop validation array to build sanatize array
			$sanatize = array();
			foreach ($validations as $item_name => $options) {
				switch ($options['type']) {
					case 'anything':
						array_push($sanatize, $item_name);
						break;
					case 'number':
						$sanatize[$item_name] = 'int';
						break;
					case 'price':
						$sanatize[$item_name] = 'float';
						break;
					case 'email':
						$sanatize[$item_name] = 'email';
						break;
					default:
						break;
				}
			}

			$validator = new FormValidator($validations, $required, $sanatize);

			if ($validator->validate($_POST)) {
				// Very important!!
				$_POST = $validator->sanatize($_POST);

				$record = Form_Model::insertPostData($con, $_POST);
				// Send Email
				sendSuccessEmail('itimphp@gmail.com', 'itimphp@gmail.com', 'itimphp@gmail.com', 'LOREM IPSUM', 'LOREM IPSUM');

				// Show thank you page
				header("Location: thankyou.php", true, 302);
			} else {
				// Echo error message
				$error_message = '<div class="alert alert-error"><ol>';
				foreach ($validator->getError() as $error) {
					$error_message .= '<li>' . $error . '</li>';
				}
				$error_message .= '</ol></div>';
				echo $error_message;
			}
		}
	}
}

require_once 'form1_view.php';